package testinit;

import java.io.FileNotFoundException;

import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.Log4jConfigurer;

public class SpringConfigInit {

	private static AbstractXmlApplicationContext abstractXmlApplicationContext;
	
	public static AbstractXmlApplicationContext getBeanFactory(){
		SpringConfigInit.abstractXmlApplicationContext = new SpringConfigInit().init();
		return SpringConfigInit.abstractXmlApplicationContext;
	}
	
	private AbstractXmlApplicationContext init(){
		try {
			Log4jConfigurer.initLogging(Log4jConfigurer.CLASSPATH_URL_PREFIX+"config/log4j.properties");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
		        new String[] {"/config/applicationContext.xml"});
		
		return context;
	}
	
	public static void closeContext(){
		SpringConfigInit.abstractXmlApplicationContext.registerShutdownHook();
	}

}
